package org.mclll.xwangplugin.resfly;

import com.bekvon.bukkit.residence.api.ResidenceApi;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.config.GlobalConfig;
import org.mclll.xwangplugin.util.PlayerTools;

import java.util.List;

/**
 * @author llB
 * @date 2024-07-09 21:41
 * @description todo
 */
public class ResFlyTimer {

    public static void start(MclllCustom plugin) {
        Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> task(plugin), 20 * 3, 20);
    }

    private static void task(MclllCustom plugin) {
        ConfigurationSection resFlyConfig = GlobalConfig.CONFIG.getConfigurationSection("resFly");
        String permission = resFlyConfig.getString("flyPermission");
        List<String> ignoreWithPerm = resFlyConfig.getStringList("ignoreWithPerm");
        for (Player player : Bukkit.getOnlinePlayers()) {
            ClaimedResidence res = ResidenceApi.getResidenceManager().getByLoc(player.getLocation());
            // 过滤指定权限的玩家
            if (player.isOp() || ignoreWithPerm.stream().allMatch(player::hasPermission)) {
                continue;
            }
            if (resAllow(resFlyConfig, res)) {
                if (!player.hasPermission(permission)) {
                    flayOn(plugin, resFlyConfig, player);
                }
                continue;
            }
            if (player.hasPermission(permission)) {
                flayOff(plugin, resFlyConfig, player);
            }
        }
    }

    private static boolean resAllow(ConfigurationSection resFlyConfig, ClaimedResidence res) {
        if (res == null) {
            return false;
        }
        List<String> resWhiteList = resFlyConfig.getStringList("resWhiteList");
        for (String reg : resWhiteList) {
            if (res.getName().matches(reg)) {
                return true;
            }
        }
        return false;
    }

    private static void flayOff(MclllCustom plugin, ConfigurationSection resFlyConfig, Player player) {
        String permission = resFlyConfig.getString("flyPermission");
        String flyCloseCommand = resFlyConfig.getString("flyCloseCommand");
        String offMsg = resFlyConfig.getString("offMsg");
        Bukkit.getScheduler().runTask(plugin, () -> {
            // 移除权限
            PlayerTools.delPermission(player.getName(), permission);
            player.sendMessage(Component.text(offMsg)
                    .color(NamedTextColor.GREEN));
            // 如果玩家正在飞行
            if (player.isFlying()) {
                // 关闭飞行
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format(flyCloseCommand, player.getName()));
                // 免疫伤害
                player.setInvulnerable(true);
                // 10秒后取消免疫伤害
                Bukkit.getScheduler().runTaskLater(plugin, () -> player.setInvulnerable(false), 20 * 10);
            }
        });
    }

    private static void flayOn(MclllCustom plugin, ConfigurationSection resFlyConfig, Player player) {
        String permission = resFlyConfig.getString("flyPermission");
        String onMsg = resFlyConfig.getString("onMsg");
        Bukkit.getScheduler().runTask(plugin, () -> {
            // 授予权限
            PlayerTools.addPermission(player.getName(), permission);
            player.sendMessage(Component.text(onMsg)
                    .color(NamedTextColor.GREEN));
        });
    }
}
